package com.fzh.bookstore.utils;

import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.Map;

public class WSXCUtils {

    public static String getUrl(String url2) {
        try {
            Document doc = Jsoup.connect(url2).get();
            String baseUrl = doc.baseUri(); //短链接转换原始链接
            System.out.println(baseUrl);
            Map<String, String> cookies = null;
            Connection.Response res1 = Jsoup.connect(baseUrl).timeout(30000).execute();
            cookies = res1.cookies(); //获取  请求响应 获取cookies
            System.out.println("cookies:"+cookies);
            System.out.println(System.currentTimeMillis());//获取当前时间错
            String shopId = baseUrl.split("shop_detail/")[1];//获取店铺的ID
            String url = baseUrl.split("com")[0]+"com"+"/service/shoppingCart/shopping_card_index.jsp?act=check_shopping_cart&_="+System.currentTimeMillis();
            System.out.println(url); //
            Connection.Response res = Jsoup.connect(url)
                    .header("Accept", "*/*")
                    .header("Accept-Encoding", "gzip, deflate")
                    .header("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .header("Cookie","token="+cookies.get("token"))
                    .header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0")
                    .timeout(10000).ignoreContentType(true).execute();//.get();
            String body = res.body();
            JSONObject json = JSONObject.parseObject(body);
            System.out.println(json);

            String newUrl = "https://"+shopId+".szwego.com/album/personal/all?&albumId="+shopId+"&searchValue=&searchImg=&startDate=&endDate=&sourceId=&slipType=1&timestamp=1609158776818&requestDataType=";
            res = Jsoup.connect(newUrl)
                    .header("Accept", "*/*")
                    .header("Accept-Encoding", "gzip, deflate")
                    .header("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
                    .header("Content-Type", "application/json;charset=UTF-8")
                    .header("Cookie","token="+cookies.get("token"))
                    .header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0")
                    .timeout(10000).ignoreContentType(true).execute();//.get();

            //&searchValue=&searchImg=&startDate=&endDate=&sourceId=&slipType=1&timestamp=1609158776818&requestDataType=
            String body1 = res.body();
           // System.out.println(body1);
            JSONObject json2 = JSONObject.parseObject(body1);
            return json2.toJSONString();
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    public static void main(String[] args) {
        WSXCUtils.getUrl("https://s.wsxc.cn/78BgT3");
    }
}
